﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace IT_Pro.Forms
{
    public partial class frmCVLapPhieuYeuCau : DevExpress.XtraEditors.XtraForm
    {
        public frmCVLapPhieuYeuCau()
        {
            InitializeComponent();
            loaddulieuvaoform("");
        }

        public frmCVLapPhieuYeuCau(string nguoiyc)
        {
            InitializeComponent();
            loaddulieuvaoform(nguoiyc);
        }
                
        private void loaddulieuvaoform(string nyc)
        {
            //load ngay thang hien hanh
            dateNgayLap.DateTime = DateTime.Now;
            DataTable dtnv = Class.NV_NhanVien.HienThiDsNhanVien();
            dtnv.Rows.InsertAt(dtnv.NewRow(), 0);
            dtnv.Rows[0]["MaNV"] = "";
            dtnv.Rows[0][1] = "[Chọn Nhân Viên]";

            cboNguoiYC.Properties.DataSource = dtnv;
            cboNguoiYC.Properties.DisplayMember = "TenNV";
            cboNguoiYC.Properties.ValueMember = "MaNV";
            cboNguoiYC.EditValue = dtnv.Rows[0][0].ToString();
            if (nyc.Length > 1)
            {
               cboNguoiYC.EditValue = nyc;
            }

            /*
            cboNguoiTH.Properties.DataSource = dtnv;
            cboNguoiTH.Properties.DisplayMember = "TenNV";
            cboNguoiTH.Properties.ValueMember = "MaNV";
            cboNguoiTH.EditValue = dtnv.Rows[0][0].ToString();
            */
            cboNguoiDuyet.Properties.DataSource = dtnv;
            cboNguoiDuyet.Properties.DisplayMember = "TenNV";
            cboNguoiDuyet.Properties.ValueMember = "MaNV";
            cboNguoiDuyet.EditValue = dtnv.Rows[0][0].ToString();

            cboLoaiYC.Properties.DataSource = Class.CV_GhiNhanYeuCau.LoadLoaiYeuCau();
            cboLoaiYC.Properties.DisplayMember = "TenLoai";
            cboLoaiYC.Properties.ValueMember = "IDLYC";
            cboLoaiYC.EditValue = "0";

            DataTable dtad = Class.CV_GhiNhanYeuCau.LoadLoaiApDung();
            cboApDung.Properties.DataSource = dtad;
            cboApDung.Properties.DisplayMember = "TenLoaiAD";
            cboApDung.Properties.ValueMember = "IDAD";
            cboApDung.EditValue = dtad.Rows[0][0].ToString();

            dateNgayKetThuc.DateTime = DateTime.Now;
        }

        private void cboApDung_EditValueChanged(object sender, EventArgs e)
        {
            if (cboApDung.EditValue.ToString() == "3")
            {
                lblKetThuc.Visible = true;
                dateNgayKetThuc.Visible = true;
            }
            else
            {
                lblKetThuc.Visible = false;
                dateNgayKetThuc.Visible = false;
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (!kiemtradulieutrenform())
            {
                MessageBox.Show("Có dữ liệu bạn chưa chọn");
            }
            else
            {
                Class.CV_GhiNhanYeuCau gnh = new Class.CV_GhiNhanYeuCau();
                gnh.NgayYC = dateNgayLap.DateTime;
                gnh.NguoiYC = cboNguoiYC.EditValue.ToString();
                gnh.NguoiTH ="";
                gnh.NguoiDuyet = cboNguoiDuyet.EditValue.ToString();
                gnh.LoaiYC =int.Parse( cboLoaiYC.EditValue.ToString());
                gnh.NoiDung = txtNoiDung.Text;
                gnh.ApDung =int.Parse( cboApDung.EditValue.ToString());
                gnh.NgayKT = dateNgayKetThuc.DateTime;
                gnh.GhiChu = txtGhiChu.Text;
                try
                {
                    gnh.ThemGhiNhanYeuCauIT();
                    MessageBox.Show("Lập phiếu thành công");
                    this.Close();
                }
                catch
                {
                    MessageBox.Show("Lỗi lập phiếu yêu cầu");

                }
            }
        }

        bool kiemtradulieutrenform()
        {
            if (cboNguoiYC.EditValue.ToString().Length < 1)
            {
                return false;
            }           
            if (cboNguoiDuyet.EditValue.ToString().Length < 1)
            {
                return false;
            }
            if (cboLoaiYC.EditValue.ToString()=="0")
            {
                return false;
            }

            return true;
        }

      
    }
}